package com.example.newdemo.view.recyclerView.grid;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.newdemo.BaseActivity;
import com.example.newdemo.R;
import com.example.newdemo.animator.AnimatorDemoActivity;
import com.example.newdemo.bean.IndexBean;
import com.example.newdemo.thread.ThreadActivity;
import com.example.newdemo.tv.FocusMoveDemoActivity;
import com.example.newdemo.tv.RecyclerTVDemoActivity;
import com.example.newdemo.utils.LogUtils;
import com.example.newdemo.view.ViewIndexActivity;
import com.example.newdemo.view.group.FlexViewActivity;
import com.example.newdemo.view.image.SvgImageActivity;
import com.example.newdemo.view.recyclerView.RecyclerViewActivity;
import com.example.newdemo.view.recyclerView.decoration.MyItemDecoration;

import java.util.ArrayList;

public class GridRecyclerViewDemoActivity extends BaseActivity {


    private RecyclerView mRecyclerView;


    private MyItemDecoration mDecoration;
    private GridLayoutManager gridLayoutManager;
    private ArrayList<IndexBean> indexList;

    private int temp = 0;
    private GridAdapter adapter;


    @Override
    protected int getLayoutId() {
        return R.layout.act_recycler_view1;
    }

    @Override
    protected void initView() {

        LinearLayout llBtn = findViewById(R.id.ll_control_btn);
        llBtn.setVisibility(View.VISIBLE);

        Button addButton = findViewById(R.id.btn_add_item);
        Button deleteButton = findViewById(R.id.btn_delete_item);


        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                indexList.add(0 , new IndexBean(temp + "  temp ",ThreadActivity.class ));
                temp++;
                adapter.notifyDataSetChanged();
                logPosition();
            }
        });

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                indexList.remove(0);
                temp++;
                adapter.notifyDataSetChanged();
                logPosition();
            }
        });

        mRecyclerView = (RecyclerView) findViewById(R.id.rv);




        gridLayoutManager = new GridLayoutManager(this, 3, LinearLayoutManager.HORIZONTAL, false);
//        gridLayoutManager = new GridLayoutManager(this ,5, LinearLayoutManager.VERTICAL , false);


        mDecoration = new MyItemDecoration(gridLayoutManager);
        mRecyclerView.addItemDecoration(mDecoration);
        //  自定义item占据的小格大小时需要重写 getSpanSize()，返回值就是占据的小格数量
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //以下代码仅为上图示例为写，具体场景中应该根据需求具体编写
                if (position == 3) {
                    return 2;
                }
                if (position == 7) {
                    return 3;
                }
                return 1;
            }

            //这个方法也很重要，但我还没搞清楚它的具体效果，从注释上来看，该方法是用于指定 item 在该行或该列上具体哪个位置，
            // 比如将GridLayoutManager设置为3行水平样式，那么第1个卡位就是在第一列的 0 位置，第2个卡位 1，一次类推。但该方法具体被调用的场景还没理清

            @Override
            public int getSpanIndex(int position, int spanCount) {
                LogUtils.i("position = " + position + " ;  spanCount " + spanCount);
                return super.getSpanIndex(position, spanCount);
            }
        });


        //官方建议说，如果延用默认的 getSpanIndxe() 的实现逻辑的话，那么建议调用下述方法来进行优化，否则每次布局计算时会很耗性能。
        gridLayoutManager.getSpanSizeLookup().setSpanIndexCacheEnabled(true);
        mRecyclerView.setLayoutManager(gridLayoutManager);


        indexList = getIndexList();

        adapter = new GridAdapter(this, indexList);
        mRecyclerView.setAdapter(adapter);


        adapter.setOnItemStateListener(new GridAdapter.OnItemStateIndexListener() {
            @Override
            public void onItemClick(View view, int position) {
                IndexBean bean = indexList.get(position);
                LogUtils.d(bean.toString());
            }
        });
    }

    @Override
    protected void initData() {

    }



    public ArrayList<IndexBean> getIndexList() {
        ArrayList<IndexBean> indexList = new ArrayList<>();
        indexList.add(new IndexBean("焦点移动", FocusMoveDemoActivity.class));
        indexList.add(new IndexBean("tv", RecyclerTVDemoActivity.class));

        indexList.add(new IndexBean("动画", AnimatorDemoActivity.class));

        indexList.add(new IndexBean("Thread", ThreadActivity.class));

        indexList.add(new IndexBean("view页面", ViewIndexActivity.class));

        indexList.add(new IndexBean("SVG解析", SvgImageActivity.class));

        indexList.add(new IndexBean("recycler", RecyclerViewActivity.class));


        indexList.add(new IndexBean("换行的view", FlexViewActivity.class));

        indexList.add(new IndexBean("text01", FlexViewActivity.class));
        indexList.add(new IndexBean("text02", FlexViewActivity.class));
        indexList.add(new IndexBean("text03", FlexViewActivity.class));
        indexList.add(new IndexBean("text04", FlexViewActivity.class));
        indexList.add(new IndexBean("text05", FlexViewActivity.class));

//        indexList.add(new IndexBean("",.class));
//        indexList.add(new IndexBean("",.class));


        return indexList;
    }


    private void logPosition() {
        for (int i = 0; i < mRecyclerView.getChildCount(); i++) {
            View view = mRecyclerView.getChildAt(i);
            int layPosi = mRecyclerView.findContainingViewHolder(view).getLayoutPosition();
            int adapterPosi = mRecyclerView.findContainingViewHolder(view).getAdapterPosition();
            int oldPosi = mRecyclerView.findContainingViewHolder(view).getOldPosition();
            LogUtils.d( " getLayoutPosition = " + layPosi  +  "; getAdapterPosition = " + adapterPosi);
        }
    }


}
